package com.wgchao.authodemo.domian.entity.base;

import javax.persistence.*;
import java.io.Serializable;

import com.wgchao.authodemo.domian.entity.base.dao.AgencyDao;
import com.wgchao.authodemo.infrastructure.exception.ParamDefectException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @作者  wgchao
 * @时间 2019-12-10 
 * @用途 
 */

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table ( name ="agency" )
public class AgencyDO  implements Serializable {

	private static final long serialVersionUID =  6639665647466350795L;

	/**
	 * id
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
   	@Column(name = "agency_id" )
	private Long agencyId;

	/**
	 * 机构名称
	 */
   	@Column(name = "agency_name" )
	private String agencyName;

	/**
	 * 备注
	 */
   	@Column(name = "remark" )
	private String remark;

	/**
	 * 创建时间
	 */
   	@Column(name = "create_time" )
	private Date createTime;

	/**
	 * 上级机构编号
	 */
   	@Column(name = "par_agency_code" )
	private String parAgencyCode;

	/**
	 * 机构编号3702----
	 */
   	@Column(name = "code" )
	private String code;

	/**
	 * 删除标志
	 */
   	@Column(name = "delete_flag" )
	private boolean deleteFlag;

	/**
	 * 删除时间
	 */
   	@Column(name = "delete_time" )
	private Date deleteTime;

   	@Transient
    private AgencyDao agencyDao;

   	private void createNewCode(){
   		if(StringUtils.isEmpty(this.parAgencyCode)){
			throw new ParamDefectException("父机构编号缺失");
		}
		this.code = agencyDao.searchMaxChild(this.parAgencyCode);
	}

	public void createExpand(){
   		createNewCode();
   		this.createTime = new Date();
   		this.deleteFlag = false;
	}



}
